package gw.com.android.presenter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.JsonUtil;

/**
 * Created by reeta.zou on 2017/5/17.
 */
public class GTDataResolve {

    private static volatile GTDataResolve mInstance;//单利引用

    /**
     * 获取单例引用
     */
    public static GTDataResolve getInstance() {
        GTDataResolve inst = mInstance;
        if (inst == null) {
            synchronized (GTDataResolve.class) {
                inst = mInstance;
                if (inst == null) {
                    inst = new GTDataResolve();
                    mInstance = inst;
                }
            }
        }
        return inst;
    }

    /**
     * 手机登录切换账号数据处理
     */
    public DataItemResult dealPhoneLoginData(Object result, int accountType) {
        DataItemResult resultObj = new DataItemResult();
        try {
            JSONObject jsonResult = new JSONObject(result.toString());
            JSONObject contextResult = jsonResult.getJSONObject("context");
            if (accountType == GTConfig.ACC_TYPE_DEMO && contextResult.has("demoCustomerList")) {
                JSONArray demoCustomerList = contextResult.getJSONArray("demoCustomerList");
                if (null != demoCustomerList && demoCustomerList.length() > 0) {
                    JsonUtil.toDataItemResult(resultObj, demoCustomerList);
                }

            } else if (accountType == GTConfig.ACC_TYPE_REAL && contextResult.has("realCustomerList")) {
                JSONArray demoCustomerList = contextResult.getJSONArray("realCustomerList");
                if (null != demoCustomerList && demoCustomerList.length() > 0) {
                    JsonUtil.toDataItemResult(resultObj, demoCustomerList);
                }
            } else {
                resultObj.message = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1012);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            resultObj.message = ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1012);
        }
        return resultObj;
    }

    /**
     * 处理开立模拟账户接口数据
     */
    public DataItemDetail dealOpenDemo(Object result) {
        DataItemDetail resultObj = new DataItemDetail();
        try {
            JSONObject jsonResult = new JSONObject(result.toString());
            if ("OK".equals(jsonResult.getString("code")) && jsonResult.has("account")) {
                JSONObject object = jsonResult.getJSONObject("account");
                if (null != object) {
                    resultObj.setBooleanValue("isOK", true);
                    JsonUtil.toDataItemDetail(resultObj, object);
                }
            } else {
                resultObj.setBooleanValue("isOK", false);
                resultObj.setStringValue("msg", ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1012));
            }
        } catch (JSONException e) {
            e.printStackTrace();
            resultObj.setBooleanValue("isOK", false);
            resultObj.setStringValue("msg", ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1012));
        }
        return resultObj;
    }
}
